<%@ page contentType="text/html; charset=UTF-8"%>
<%@include file="/html/portlet/chart/admin/init.jsp"%>


<%

	String randomNamespace = PortalUtil.generateRandomKey(request, "taglib_ui_input_asset_links_page") + StringPool.UNDERLINE;

	String eventName = randomNamespace + "selectChartTemplate";

	Option option = null;
	long optionId = ParamUtil.getLong(request, "optionId");
	String templateName = StringPool.BLANK;
	String templateId = StringPool.BLANK;
	try {
		if (optionId > 0) {
			option = OptionLocalServiceUtil.getOption(optionId);
			if(option.getChartTemplateId()>0){
				ChartTemplate template = ChartTemplateLocalServiceUtil.getChartTemplate(option.getChartTemplateId());
				templateName = template.getName();
				templateId = StringUtil.valueOf(template.getChartTemplateId());
			}
		}
	}
	catch (NoSuchChartTemplateException nsct) {
	}
	
%>
<%
PortletURL selectTemplateUrl = renderResponse.createRenderURL();
selectTemplateUrl.setParameter("mvcPath", "/html/portlet/chart/template/select_template.jsp");
selectTemplateUrl.setParameter("eventName", eventName);
selectTemplateUrl.setWindowState(new WindowState("pop_up"));
%>
<div class="control-group">
		<aui:input name="chartTemplateId" type="hidden" value="<%=templateId %>"/>
		<aui:input inlineField="<%=true %>" label="chart.baseconfig.select.Template" name="templateNameDisplay" id="templateNameDisplay" required="<%= true %>" disabled="true"  value="<%=templateName %>"/>
		<liferay-ui:icon
				cssClass="charttemplate-selector"
				iconCssClass="icon-search"
				label="<%= true %>"
				linkCssClass="btn"
				message="select"
				url="javascript:;" 
			/>
	
</div>
<aui:script use="aui-base,escape,liferay-search-container">
	A.getBody().delegate(
		'click',
		function(event) {
			event.preventDefault();

			Liferay.Util.selectEntity(
				{
					dialog: {
						constrain: true,
						modal: true,
						destroyOnHide: true
					},
					eventName: '<%= eventName %>',
					id: '<%= eventName %>' + event.currentTarget.attr('id'),
					title: '<%=LanguageUtil.get(pageContext, "chart.baseconfig.select.Template") %>',
					uri: '<%=selectTemplateUrl %>'
				},
				function(event) {
					//var name = A.Lang.String.unescapeEntities(event.name);
					A.one('#<portlet:namespace />chartTemplateId').val(event.charttemplateid);
					A.one('#<portlet:namespace />templateNameDisplay').val(event.name);
					
				}
			);
		},
		'.charttemplate-selector a'
	);
</aui:script>